#!/bin/bash

#######################################################################
# Launcher for DNS Blocklist script by sfeakes
#
# Launcher allows additional sources from a source file
# to prevent long entries in fcrontab
#######################################################################

#######################################################################
  revision="dnsbl_laucher.sh v0.2"                  # do not comment out
# Last updated: February 11 2018 by maloe
# Author: maloe
# Source: notabug.org/maloe/DSN_Blocklist_Launcher
# License: GNU GPL v3
# Copyright 2018 maloe
#######################################################################

#######################################################################
# Options, Filenames

default_sourcelist="1,2,3,5,7,8,12,13,14"			# Selected default sources
dns="0.0.0.0"							# DNS Argument, for experimental nxgomain mode set to refuse, static, always_refuse or always_nxdomain

thisfolder=$(dirname $(readlink -f $0))				# Actual path
scriptpath=${thisfolder}/dns/dns_blocklist.sh			# Path to dns_blocklist.sh

custom_sourcefile=${thisfolder}/dns/sourcelist			# Path to custom sources file
blacklistfile=${thisfolder}/dns/blacklist.hosts			# Path to custom blacklist
whitelistfile=${thisfolder}/dns/whitelist.hosts			# Path to custom whitelist
										
logfile=${thisfolder}/dns/blocklist.log				# Path to log file (useful when starting via cron)
blocklistconf=/etc/unbound/local.d/blocklist.conf		# Path to blocklist.conf

#######################################################################
# NO  NEED TO EDIT ANYTHING BELOW
#######################################################################

	print_help() {											# Help info
		echo "Usage: dnsbl_launcher.sh [OPTION]"
		echo "Launcher for DNS Blocklist Script by sfeakes"
		echo
		echo "Options:"
		echo "  -ls, --sourcelist           List sources"
		echo "  -lb, --blocklist search     List and search unbound conf"
		echo "  -ll, --logfile              List log"
		echo "  -h,  --help                 Show this help"
		echo
	}

case $1 in 
	-ls | --sourcelist) {										# List sources
		echo -e "### default sources:\n$default_sourcelist\n"
		cat $custom_sourcefile
	};;
	-lb | --blocklist) {										# List blocks
		if [[ $2 ]]; then 
			cat $blocklistconf | grep -i "$2" --color=auto
		else
			cat $blocklistconf
		fi
	};;
	-ll | --logfile) {										# List log
		cat $logfile
	};;
	-h | --help) {											# Show help
		print_help  
	};;
	*) {
		if [[ -f $custom_sourcefile ]]; then							# Custom source file available?
			for url in `cat ${custom_sourcefile} | grep -v '^#' | cut -f1`; do		# Ignore comment lines in custom source file
				custom_sourcelist=${custom_sourcelist},$url				# Custom URLs into one double quoted and comma separated line
			done
			custom_sourcelist=`echo ${custom_sourcelist} | sed 's/^,//'`			# Remove first character comma 	
		fi
		sourcelist=`echo "-s ${default_sourcelist}","${custom_sourcelist}" | sed 's/^-s ,/-s /; s/,,/,/g; s/,$//; /^-s $/d' `		# Generate sourcelist argument

		if 	[[ -f ${blacklistfile} ]]; then blacklist="-b "${blacklistfile}; fi		# Generate blacklist argument
		if 	[[ -f ${whitelistfile} ]]; then whitelist="-w "${whitelistfile}; fi		# Generate whitelist argument

		if [[ ${logfile} ]]; then								# Create log file if logfile is given
			echo $revision >${logfile}
			echo "last execution on "`date` >>${logfile}
			echo "commandline:" >>${logfile} 
			echo "$scriptpath $sourcelist $blacklist $whitelist -r $dns" >>${logfile} 
			echo >>${logfile} 
		fi

		$scriptpath $sourcelist $blacklist $whitelist -r $dns 
	};;
esac

exit 0
